package com.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.bean.Employee;
import com.chen.common.R;
import com.chen.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录验证
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public R<Employee> lonIn(@RequestBody Employee emp, HttpSession session){
        return employeeService.logIn(emp,session);
    }

    /**
     * 后台退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logOut(HttpSession session){
        return employeeService.logOut(session);
    }

    /**
     * 新增员工
     * @param emp
     * @param session
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee emp, HttpSession session){
        return employeeService.add(emp,session);
    }

    /**
     * 分页条件查询
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @param name 员工名字
     * @return 分页的数据
     */
    @GetMapping("/page")
    public R<Page<Employee>> findPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer pageSize,String name){
        return employeeService.conditionPage(page,pageSize,name);
    }

    /**
     * 修改用户
     * @param emp
     * @return
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee emp){
        return employeeService.updateEmployee(emp);
    }

    /**
     *根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable long id){
        return employeeService.findById(id);
    }
}
